import React, { useCallback, useLayoutEffect, useState } from "react";
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UserOutlined,
} from "@ant-design/icons";
import { Button, Layout, Menu, theme } from "antd";
import { Outlet, useNavigate } from "react-router-dom";
import { tabbar } from "./router";

const { Header, Sider, Content } = Layout;

const App: React.FC = () => {
  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();
  const [luyou, setluyou] = useState([
    {
      key: "1",
      icon: <UserOutlined />,
      label: "nav 1",
    },
  ]);
  const jd = useCallback((routes: any[]) => {
    const items = routes.map((item) => {
      const res: any = {};
      res.key = item.path;
      if (item.icon) {
        res.icon = item.icon;
      }
      res.label = item.label;
      if (item.children && item.children.length >= 0) {
        res.children = jd(item.children);
      }
      return res;
    });
    return items;
  }, []);
  useLayoutEffect(() => {
    setluyou(jd(tabbar));
  }, []);
  const navigate = useNavigate();
  const xd = useCallback((obj) => {
    navigate(obj.key);
  }, []);
  return (
    <Layout style={{ height: "100vh" }}>
      <Sider trigger={null} collapsible collapsed={collapsed}>
        <div className="demo-logo-vertical" />
        <Menu
          theme="dark"
          mode="inline"
          defaultSelectedKeys={["/"]}
          items={luyou}
          onClick={xd}
        />
      </Sider>
      <Layout>
        <Header style={{ padding: 0, background: colorBgContainer }}>
          <Button
            type="text"
            icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
            onClick={() => setCollapsed(!collapsed)}
            style={{
              fontSize: "16px",
              width: 64,
              height: 64,
            }}
          />
        </Header>
        <Content
          style={{
            margin: "24px 16px",
            padding: 24,
            minHeight: 280,
            background: colorBgContainer,
            borderRadius: borderRadiusLG,
          }}
        >
          <Outlet />
        </Content>
      </Layout>
    </Layout>
  );
};

export default App;
